﻿using System;
using System.Collections.Generic;
using System.Linq;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using TiaoTaoHelper.Wtm.Models;
using HengTong.Model;
using HengTong.Model.Db.Hr.Bill;
using HengTong.Model.Extension;

namespace HengTong.ViewModel.Hr.SysHrStaffNeedRequestBillVms
{
    public partial class
        SysHrStaffNeedRequestBillListVm : BaseListVm<SysHrStaffNeedRequestBillView, SysHrStaffNeedRequestBillSearcher>
    {
        protected override IEnumerable<IGridColumn<SysHrStaffNeedRequestBillView>> InitGridHeader()
        {
            return new List<GridColumn<SysHrStaffNeedRequestBillView>>
            {
                this.MakeGridHeader(x => x.BillCode),
                this.MakeGridHeader(x => x.EntryGroupName),
                this.MakeGridHeader(x => x.EntryJobPositionName),
                this.MakeGridHeader(x => x.EntryHireNumber),
                this.MakeGridHeader(x => x.EntryApplyReason),
                this.MakeGridHeader(x => x.EntryExpectedDate),
                this.MakeGridHeader(x => x.EntryJobRequirement),
                this.MakeGridHeader(x => x.EntryEducationRequirement),
                this.MakeGridHeader(x => x.EntryMajorRequirement),
                this.MakeGridHeader(x => x.EntryGenderRequirement),
                this.MakeGridHeader(x => x.EntryAgeRequirement),
                this.MakeGridHeader(x => x.EntryOtherRequirement),
                this.MakeGridHeader(x => x.BillStatus),
                this.MakeGridHeader(x => x.WorkflowStatus),
                this.MakeGridHeader(x => x.GroupName),
                this.MakeGridHeader(x => x.OrgName),
                this.MakeGridHeader(x => x.Approver),
                this.MakeGridHeader(x => x.ApprovalTime),
                this.MakeGridHeader(x => x.Remark),
                this.MakeGridHeader(x => x.CanApprove).SetHide().SetFormat((a, b) =>
                {
                    if (a.CanApprove)
                    {
                        return "true";
                    }
                    else
                    {
                        return "false";
                    }
                }),
                this.MakeGridHeaderAction(width: 200)
            };
        }

        public override void AfterDoSearcher()
        {
            if (Wtm.LoginUserInfo != null)
            {
                List<SysWorkflow> ids = GetMyApproves();
                if (ids.Any())
                {
                    foreach (var item in EntityList)
                    {
                        var m = ids.Find(x => x.ModelId == item.GetID().ToString());
                        if (m != null && item.WorkflowStatus == WorkflowStatus.审核中)
                        {
                            item.CanApprove = true;
                        }
                    }
                }
            }

            EntityList.ForEach(e =>
            {
                e.CreateBy = Wtm.FormatUsername(e.CreateBy);
                e.UpdateBy = Wtm.FormatUsername(e.UpdateBy);
                e.Approver = Wtm.FormatUsername(e.Approver);
            });

            base.AfterDoSearcher();
        }

        public override IOrderedQueryable<SysHrStaffNeedRequestBillView> GetSearchQuery()
        {
            var query = DC.Set<SysHrStaffNeedRequestBill>()
                .Include<SysHrStaffNeedRequestBill, ICollection<SysHrStaffNeedRequestBillEntry>>(x => x.StaffNeedSubs)
                .Include(x => x.StaffNeedSubs)
                .ThenInclude(s => s.Org)
                .Include(x => x.StaffNeedSubs)
                .ThenInclude(s => s.Group)
                .Include(x => x.StaffNeedSubs)
                .ThenInclude(s => s.JobPosition)
                .CheckContain(Searcher.Approver != null ? new List<string> { Searcher.Approver } : null,
                    x => x.Approver)
                .CheckBetween(Searcher.ApprovalTime?.GetStartTime(), Searcher.ApprovalTime?.GetEndTime(),
                    x => x.ApprovalTime, includeMax: false)
                .CheckEqual(Searcher.GroupId, x => x.GroupId)
                .CheckEqual(Searcher.OrgId, x => x.OrgId)
                .CheckContain(Searcher.BillCode != null ? new List<string> { Searcher.BillCode } : null,
                    x => x.BillCode)
                .CheckEqual(Searcher.BillStatus, x => x.BillStatus)
                .CheckEqual(Searcher.WorkflowStatus, x => x.WorkflowStatus)
                .SelectMany(x => x.StaffNeedSubs.DefaultIfEmpty(), (bill, entry) => new SysHrStaffNeedRequestBillView
                {
                    ID = bill.ID,
                    Approver = bill.Approver,
                    ApprovalTime = bill.ApprovalTime,
                    GroupName = bill.Group.GroupName,
                    OrgName = bill.Org.Name,
                    BillCode = bill.BillCode,
                    BillStatus = bill.BillStatus,
                    WorkflowStatus = bill.WorkflowStatus,
                    Remark = bill.Remark,
                    CreateTime = bill.CreateTime,

                    // 子表字段
                    EntryHireNumber = entry != null ? entry.HireNumber : 0,
                    EntryApplyReason = entry != null ? entry.ApplyReason : "",
                    EntryExpectedDate = entry != null ? entry.ExpectedDate : null,
                    EntryJobRequirement = entry != null ? entry.JobRequirement : "",
                    EntryEducationRequirement = entry != null ? entry.EducationRequirement : null,
                    EntryMajorRequirement = entry != null ? entry.MajorRequirement : "",
                    EntryGenderRequirement = entry != null ? entry.GenderRequirement : null,
                    EntryAgeRequirement = entry != null ? entry.AgeRequirement : "",
                    EntryOtherRequirement = entry != null ? entry.OtherRequirement : "",
                    EntryGroupName = entry != null && entry.Group != null ? entry.Group.GroupName : "",
                    EntryOrgName = entry != null && entry.Org != null ? entry.Org.Name : "",
                    EntryJobPositionName = entry != null && entry.JobPosition != null ? entry.JobPosition.Name : ""
                })
                .OrderByDescending(x => x.CreateTime);

            return query;
        }
    }

    public class SysHrStaffNeedRequestBillView : SysHrStaffNeedRequestBill
    {
        [Display(Name = "_Admin.GroupName")] public String GroupName { get; set; }
        [Display(Name = "公司名称")] public String OrgName { get; set; }
        public bool CanApprove { get; set; }

        // 子表相关字段 - 使用Entry前缀
        [Display(Name = "明细招聘人数")] public int EntryHireNumber { get; set; }
        [Display(Name = "明细申请原因")] public String EntryApplyReason { get; set; }
        [Display(Name = "明细到岗时间")] public DateTime? EntryExpectedDate { get; set; }
        [Display(Name = "明细岗位要求")] public String EntryJobRequirement { get; set; }
        [Display(Name = "明细学历要求")] public EducationLevel? EntryEducationRequirement { get; set; }
        [Display(Name = "明细专业要求")] public String EntryMajorRequirement { get; set; }
        [Display(Name = "明细性别要求")] public Gender? EntryGenderRequirement { get; set; }
        [Display(Name = "明细年龄要求")] public String EntryAgeRequirement { get; set; }
        [Display(Name = "明细其他要求")] public String EntryOtherRequirement { get; set; }
        [Display(Name = "明细部门名称")] public String EntryGroupName { get; set; }
        [Display(Name = "明细公司名称")] public String EntryOrgName { get; set; }
        [Display(Name = "明细岗位名称")] public String EntryJobPositionName { get; set; }
    }
}